<?php
namespace Admin\Controller;
use Think\Controller;
/*
 * 内容管理控制器
 */
 class ContentController extends  IsLoginController{
 	
	protected $tableName = 'defaultonmodel'; 
	 /**
	  * 文章类表
	  */
	  public function index(){
	  	$order = I('get.order')?I('get.order'):'desc';
		$ariticleCount = M('defaultonmodel')->order('aaddtime ' .$order)->where("recycle=0")->count();
		$Page = new \Think\Page($ariticleCount,C('PAGE_SIZE'));// 实例化分页类 传入总记录数和每页显示的记录数(15)
		$Page->setConfig('header','<div class="message">共<i class="blue">%TOTAL_ROW%</i>条记录，当前显示第&nbsp;<i class="blue">%NOW_PAGE%&nbsp;</i>页，共&nbsp;<i class="blue">%TOTAL_PAGE%&nbsp;</i>页</div>');
	    $Page->setConfig('prev','');
	    $Page->setConfig('next','');
	    $Page->setConfig('last','末页');
	    $Page->setConfig('first','首页');
	    $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
		$show = $Page->show();// 分页显示输出
		
	  	$article = M('defaultonmodel')->order('aaddtime ' .$order)->limit($p,C('PAGE_SIZE'))->where("recycle=0")->select();
		$this->assign('article',$article)->assign('page',$show);
	  	$this->display();
	  }
	
 	
	/**
	 * 发布内容
	 */
	 public function add(){

	 	$cid = I('get.cid','0','int');
		$fieldData = $this->getField($cid);
		//提交数据处理
		if(IS_POST){
			$this->tableName = $fieldData['modelField']['mmodel_name'];
			
			$MModel = D('defaultonmodel');
			if(!$MModel->addContent($fieldData['modelField']['mmodel_name'],$fieldData['modelField']['mid'])){
				$this->error($MModel->getError());
				return false;
			}
			$this->success('添加成功');
		}

		$modelField = $fieldData['modelField'];
		//名称、字段分配视图
		$this->assign('modelField',$modelField);
		//将字符串分配试图
		$this->assign('str',$fieldData['str']);
	 	$this->display();
		
	 }
	 
	/**
	 * 发布内容
	 */
	 public function post(){
		
		//提交数据处理
		if(IS_POST){
			$cid = I('post.cid');
			
			$this->tableName = $selfTable;
			$MModel = D('defaultonmodel');
			if(!$MModel->addContent($selfTable,$mid)){
				$this->error($MModel->getError());
				return false;
			}
			$this->success('添加成功');
		}
		//查询分类
		$category = M('category')->field('cid,name,duosute_model_mid')->select();
	
		$this->assign('category',$category);
	 	$this->display();
		
	 }
	
	 /**
	 * 修改内容
	 */
	 public function edit(){
	 	
		//获取栏目id 将模型字段 分配试图调用对应发布方法
	 	$cid = I('get.cid','0','int');
	 	$aid = I('get.aid','0','intval');

		//通过栏目cid 查询栏目模型id 在模型中查询模型对应字段  查询表字段 
		$modelField = M('category')->join("duosute_model on duosute_category.duosute_model_mid = duosute_model.mid ")->where("cid = '{$cid}'")->field('name,mfield,mdefault_field,mmodel_name,mid,cid')->find();
		
		//如果存在独立表 就查询信息
		if($modelField['mmodel_name'] != ''){
			//用户的的模型字段名 查询 模型字段		
			$sql = "SHOW FULL FIELDS FROM duosute_".$modelField['mmodel_name'];
			//执行组合语句查询字段
			$ModelField = M()->query($sql);
			$selfTable = $modelField['mmodel_name'];
			$mid = $modelField['mid'];	
			//查询当前文章id在独立字段表中的文章信息
			$fieldData = M($modelField['mmodel_name'])->where("aid='{$aid}'")->find();
		}
		$str = '';
				//查询文章信息分配视图
//		$articleData = M('defaultonmodel')->where("id = '{$aid}'")->find();	//默认表中内容信息查询	
		$articleData = M('defaultonmodel')->join('duosute_'.$selfTable .' on duosute_'.$selfTable.'.aid = duosute_defaultonmodel.id')->where("aid = '{$aid}'")->find();	//默认表中内容信息查询	

		//提交数据处理
		if(IS_POST){
			$this->tableName = $selfTable;
			$MModel = D('defaultonmodel');
			if(!$MModel->editContent($selfTable,$mid)){
				$this->error($MModel->getError());
				return false;
			}
			$this->success('内容修改成功');
		}
		
		
		foreach($ModelField as $k => $v ){
			$fieldKey =  $v['field'];
			//组合字符串  //过滤默认字段
			if($v['field'] != 'id' && $v['field'] != 'cid'){
			     //获取input 文字  //获取input 类型	
				$Fname = strstr($v['comment'],'|',true );//提示文字
				//后部截取
				$afterword = ltrim(strstr ($v['comment'],'|'),'|');
				//输入框类型
				$Fword = strstr($afterword,'|',ture );
//				$Fword = array_filter($Fword);
//				echo $Fword;
				//默认值
				$defaultValue= trim(strstr($afterword,'|' ),'|');
				//当行文本输入框
				if($Fword =='text'){					
					$str .= "<tr>";
					$str .= "<td>".$Fname."</td>";
					$str .= "<td><input type='".$Fword."' name='".$v['field']."' /></td>";
					$str .= "</tr>";
				}
				//多行文本输入框
				if($Fword =='textarea'){
////					$str .= "<tr>";
////					$str .= "<td>".$Fname."</td>";
//////					$str .= "<td><textarea type='".$Fword."' name='".$v['field']."' /></textarea></td>";
//					$str .= "</tr>";
//					$str .= "<td>".$Fname."</td>";
					$str .= "<tr>";
					$str .= "<td>".$Fname."</td>";
					$str .= "<td><script id='".$v['field']."' name='".$v['field']."'  type='text/plain' style='width: 650px; z-index: 1;'> ".$articleData[$fieldKey]."</script><script type='text/javascript'>var ue = UE.getEditor('".$v['field']."');</script>";
					$str .= "</tr>";
				}
				//附件类型
				if($Fword =='file'){
					$str .= "<tr>";
					$str .= "<td>".$Fname."</td>";
					$str .= "<td><input type='".$Fword."' name='".$v['field']."' /></td>";
					$str .= "</tr>";
				}
				//下拉框类型
				if($Fword =='option'){
					$choose = array_filter(explode(',', $defaultValue));
					$str .= "<tr>";
					$str .= "<td>".$Fname."</td><td>"; 
					$str .= "<select name='".$v['field']."'>";
					foreach($choose as $kk => $vv){
					$str .= "<option value='".$vv."'>".$vv."</option>";
					}
					$str .= "</select>";
					$str .= "</td></tr>";	
				}
				//单选
				if($Fword =='radio'){
					//取出默认值分配
					$choose = explode(',', $defaultValue);
					$str .= "<tr>";
					$str .= "<td>".$Fname."</td><td>"; 
					foreach($choose as $kk => $vv){
						$str .= "<input type='".$Fword."' name='".$v['field']."' value=".$vv." />".$vv."";
					}				
					$str .= "</td></tr>";					
				}
				//多选
				if($Fword =='checkbox'){
					//取出默认值分配
					$choose = explode(',', $defaultValue);
					$str .= "<tr>";
					$str .= "<td>".$Fname."</td><td>"; 
					foreach($choose as $kk => $vv){
						$str .= "<input type='".$Fword."' name='".$v['field']."[]' value=".$vv." />".$vv."";
					}				
					$str .= "</td></tr>";	
				}
				//图片集上传
				if($Fword =='images'){
//					$str .= "<tr>";
//					$str .= "<td>".$Fname."</td>";
					$str .= "<td class='imagesInput'><input type='hidden'  /></td>";
//					$str .= "</tr>";
				}
			}
		}
		
		//查询文章信息分配视图
//		$articleData = M('defaultonmodel')->where("id = '{$aid}'")->find();	//默认表中内容信息查询	

		if(isset($articleData['ipic'])){
			$ipicData = array_filter(explode(',', $articleData['ipic']));
			$this->assign('ipicData',$ipicData);
		}
		//查询tag标签重组 
		$tagData = M("tagarticle")->where("duosute_article_aid='{$aid}'")->field('id')->select();
		$tagData = M("tagarticle")->join('duosute_tag on duosute_tag.tid = duosute_tagarticle.tagid','RIGHT')->where("duosute_article_aid='{$aid}'")->getField('tname',true);
		$tagData = implode(',', $tagData);
		$this->assign('tagData',$tagData);
//		if(isset($tagData[0]['tname']) && $tagData[0]['tname'] !== ''){
//			foreach($tagData as $k => $v){										//下标0存在 并且不为空
//				$tagstr[$k] = $v['tname'];
//			}	
//			$tagstr = implode(',', $tagstr);
//			$this->assign('tagstr',$tagstr);
//		}
		//组合文章属性
		if($articleData['aattr'] != ''){
			$attrData = explode(',', $articleData['aattr']);
			$this->assign('attrData',$attrData);
		}
		//查询栏目 可提供发布选择发布栏目 需要动态改变字段
	 	$cateList = M("category")->select();		//模型相同的栏目才能更改
	 	foreach($cateList as $k => $v){
	 		if($v['duosute_model_mid'] == $modelField['mid']){
	 			$cateChooseList[] = $v;
	 		}
	 	}
		//名称、字段分配视图
		$this->assign('modelField',$modelField)->assign('cateList',$cateList)->assign('articleData',$articleData)->assign('str',$str)->assign('cateChooseList',$cateChooseList);		
		
		//将字符串分配试图
	 	$this->display();
		
	 }
	 
	 /**
	  * 删除内容
	  */
	 public function del(){
		//提交数据处理
		if(IS_GET){
			$MModel = D('defaultonmodel');
			if(!$MModel->delContent($selfTable)){
				$this->error($MModel->getError());
				return false;
			}
			$this->success('内容删除成功');
		}
	 }
	 
	 /**
	  * 上传图片 异步uploadify 方法
	  */
	public function uploadImg(){
		if(IS_POST){		
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath  =     'model_images/big/'; // 设置附件上传目录
		$upload->saveName  =    'duosute_'.date('ymdHis');
		$upload->autoSub   = 	true;
		$upload->callback  =    false;
		$upload->subName   =    false;
		$file =  $upload->upload();
//		if(!$info) {// 上传错误提示错误信息        
//				echo $upload->getError();
//		    }else{// 上传成功 获取上传文件信息        
//		    	 echo $info['savepath'].$info['savename'];
//			}
			//缩略图处理  一张图缩了三张图
			
//			echo "<script>console.log(".$file.")</script>";
			
			$path =  'Uploads/'.$file['Filedata']['savepath'].$file['Filedata']['savename'];//原图样式  大图样式
//			$this->ajaxReturn($path);
			
			$thumbPath = 'Uploads/'.$file['Filedata']['savepath'].'thumb/100X100_'.$file['Filedata']['savename']; 	//如果批量上传 	$file[0]['savepath'] 加0	
			$thumbPath= str_replace('big/', '', $thumbPath); //组合路径   缩图图路径样式  Uploads/model_images/thumb/100X100_duosute_150812155815.png
//			$this->ajaxReturn($thumbPath);
			$img =  new \Think\Image();
			$img->open($path);
			$img->thumb(100, 100,\Think\Image::IMAGE_THUMB_FIXED)->save($thumbPath);
			
			//循环组合图片路径 返回图片名
//			foreach($file as $k =>$v){
//				$data[$k]['fileName']= $v['savename'];
//			}
			$data['path'] = 'Uploads/'.$file['Filedata']['savepath'];
			$data['name'] = $file['Filedata']['savename'];
			
			$this->ajaxReturn($data);
		}	
//			$this->display();
	}
	
		public function upload(){
			$this->display();
		}
		
		/**
		 * 文件删除
		 */
		public function romvefile(){
			$file = I('post.filename');
			unlink($file);
			if(file_exists($file)){
			echo '0';
			} else {
			echo '1';
			}

		}
		
		public function getField($cid){
			$modelField = M('category')->join("duosute_model on duosute_category.duosute_model_mid = duosute_model.mid ")->where("cid = '{$cid}'")->field('mid,name,mfield,mdefault_field,mmodel_name')->find();
			$data['modelField'] = $modelField;
			//用户的的模型字段名 查询 模型字段		
			$sql = "SHOW FULL FIELDS FROM duosute_".$modelField['mmodel_name'];
			//执行组合语句查询字段
			$ModelField = M()->query($sql);
//			$data['selfTable'] = $modelField['mmodel_name'];
//			$data['mid'] = $modelField['mid'];
			$str = '';

			foreach($ModelField as $k => $v ){
						
				//组合字符串  //过滤默认字段
				if($v['field'] != 'id' && $v['field'] != 'cid'){
				     //获取input 文字  //获取input 类型	
					$Fname = strstr($v['comment'],'|',true );//提示文字
					//后部截取
					$afterword = ltrim(strstr ($v['comment'],'|'),'|');
					//输入框类型
					$Fword = strstr($afterword,'|',ture );
	//				$Fword = array_filter($Fword);
	//				echo $Fword;
					//默认值
					$defaultValue= trim(strstr($afterword,'|' ),'|');
					//当行文本输入框
					if($Fword =='text'){					
						$str .= "<tr>";
						$str .= "<td>".$Fname."</td>";
						$str .= "<td><input type='".$Fword."' name='".$v['field']."' /></td>";
						$str .= "</tr>";
					}
					//多行文本输入框
					if($Fword =='textarea'){
	////					$str .= "<tr>";
	////					$str .= "<td>".$Fname."</td>";
	//////					$str .= "<td><textarea type='".$Fword."' name='".$v['field']."' /></textarea></td>";
	//					$str .= "</tr>";
	//					$str .= "<td>".$Fname."</td>";
						$str .= "<tr>";
						$str .= "<td>".$Fname."</td>";
						$str .= "<td><script id='".$v['field']."' name='".$v['field']."' type='text/plain' style='width: 650px; z-index: 1;'> </script><script type='text/javascript'>var ue = UE.getEditor('".$v['field']."');</script>";
						$str .= "</tr>";
					}
					//附件类型
					if($Fword =='file'){
						$str .= "<tr>";
						$str .= "<td>".$Fname."</td>";
						$str .= "<td><input type='".$Fword."' name='".$v['field']."' /></td>";
						$str .= "</tr>";
					}
					//下拉框类型
					if($Fword =='option'){
						$choose = array_filter(explode(',', $defaultValue));
						$str .= "<tr>";
						$str .= "<td>".$Fname."</td><td>"; 
						$str .= "<select name='".$v['field']."'>";
						foreach($choose as $kk => $vv){
						$str .= "<option value='".$vv."'>".$vv."</option>";
						}
						$str .= "</select>";
						$str .= "</td></tr>";	
					}
					//单选
					if($Fword =='radio'){
						//取出默认值分配
						$choose = explode(',', $defaultValue);
						$str .= "<tr>";
						$str .= "<td>".$Fname."</td><td>"; 
						foreach($choose as $kk => $vv){
							$str .= "<input type='".$Fword."' name='".$v['field']."' value=".$vv." />".$vv."";
						}				
						$str .= "</td></tr>";					
					}
					//多选
					if($Fword =='checkbox'){
						//取出默认值分配
						$choose = explode(',', $defaultValue);
						$str .= "<tr>";
						$str .= "<td>".$Fname."</td><td>"; 
						foreach($choose as $kk => $vv){
							$str .= "<input type='".$Fword."' name='".$v['field']."[]' value=".$vv." />".$vv."";
						}				
						$str .= "</td></tr>";	
					}
					//图片集上传
					if($Fword =='images'){
	//					$str .= "<tr>";
	//					$str .= "<td>".$Fname."</td>";
						$str .= "<td class='imagesInput' style='display:none;'><input type='hidden'  /></td>";
	//					$str .= "</tr>";
					}
				}
			}
			$data['str'] = $str;
			return  $data;
		}
	 
	 /**
	  * 异步获取表单
	  */
	 public function getFieldStr(){
	 	$cid = I('post.cid');
		$str = $this->getField($cid);
		echo $str['str'];die;
		$this->ajaxReturn($str);
	 }
	 
	/**
	 * 回收站
	 */
	public function recycle(){
		$article = M('defaultonmodel')->limit(10)->where("recycle=1")->select();
		$this->assign('article',$article);
		$this->display();
	}
	 
	 
 }
